package ru.sushka.lunch.gui.controllers;

import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableItem;

import ru.sushka.lunch.enitiy.Course;
import ru.sushka.lunch.gui.AbstractMenuController;
import ru.sushka.lunch.gui.AbstractMenuModel;
import ru.sushka.lunch.gui.views.MenuForm;

public class MenuController implements AbstractMenuController {
    
    private Calendar expiryDate;
    private AbstractMenuModel menuModel;
    private MenuForm menuForm;
    
    private Map<TableItem, Course> courseMap;
    private Map<TableItem, Course> menuMap;
    
    
    public MenuController(AbstractMenuModel menuModel){
        this.menuModel = menuModel;
                
        courseMap = new HashMap<TableItem, Course>();
        menuMap = new HashMap<TableItem, Course>();
        
        expiryDate = Calendar.getInstance();
        //TODO: properties!
        expiryDate.add(Calendar.DAY_OF_MONTH, 2);
        
        menuForm = new MenuForm(this);
        menuForm.open();  
    }
    
    @Override
    public void addMenuItem(TableItem courseTableSelectedItem) {
        Course selectedCourse = courseMap.get(courseTableSelectedItem);
        if(!menuMap.containsValue(selectedCourse)){
            menuModel.addMenuItem(selectedCourse);     
            loadMenuList();
        }
    }

    @Override
    public void removeMenuItem(TableItem menuTableSelectedItem) {    
        Course selectedCourse = menuMap.get(menuTableSelectedItem);
        if(menuMap.containsValue(selectedCourse)){
            menuModel.removeMenuItem(selectedCourse);
            loadMenuList();
        }
    }

    @Override
    public void loadMenuList() {
        Table menuTable = menuForm.getMenuTable();
        menuTable.removeAll();
        menuMap.clear();
        List<Course> menuList = menuModel.getMenuList();
        if(!menuList.isEmpty()){
            for (Course course : menuList) {
                TableItem item = new TableItem(menuTable, 0);
                item.setText(0, course.getCategoryName());
                item.setText(1, course.getName());
                item.setText(2, String.valueOf(course.getPrice()));
                menuMap.put(item, course);
            }
        }
    }
    
    @Override
    public void loadMenuList(Calendar expiryDate) {
        Table menuTable = menuForm.getMenuTable();
        menuTable.removeAll();
        menuMap.clear();
        List<Course> courseList = menuModel.getMenuList();
        if(!courseList.isEmpty()){
            for (Course course : courseList) {
                TableItem item = new TableItem(menuTable, 0);
                item.setText(0, course.getCategoryName());
                item.setText(1, course.getName());
                item.setText(2, String.valueOf(course.getPrice()));
                menuMap.put(item, course);
            }
        }
    }

    @Override
    public void loadCourseList() {
        Table courseTable = menuForm.getCourseTable();
        courseTable.removeAll();
        courseMap.clear();
        for(Course course: menuModel.getCourseList()){          
            TableItem item = new TableItem(courseTable,0);
            item.setText(0,course.getCategoryName());
            item.setText(1,course.getName());
            item.setText(2,String.valueOf(course.getPrice()));
            courseMap.put(item, course);
        }
    }

    @Override
    public void setExpiryDate(Calendar expiryDate) {
        this.expiryDate = expiryDate;
    }

    @Override
    public Calendar getExpiryDate() {
        return this.expiryDate;
    }

    @Override
    public void saveMenuList() {
        menuModel.saveMenuList(expiryDate);
    }

}
